package Errors;
import java.util.*;
import java.io.*;

/*******************************************************************************
 * Class containing and managing the browser errors.
 * @author Tudor Pascu
 * @version FINAL
 *******************************************************************************/
public class ErrorLog
{
    private ArrayList<String> listOfErrors;//List of errors.
    private boolean hasErrors;//True if errors are in the list, false otherwise.

    /***************************************************************************
     * Constructor for the error log. The error log will keep track of errors in
     * all the classes. These errors will be displayed in the GUI and where the
     * program is located.
     ***************************************************************************/
    public ErrorLog()
    {
        listOfErrors = new ArrayList<String>();
        hasErrors = false;
    }

    /***************************************************************************
     * Adds an error to the list.
     * @param error Description of the error.
     ***************************************************************************/
    public void logError(String error)
    {
        listOfErrors.add(error);
        hasErrors = true;
    }

    /***************************************************************************
     * Returns true if errors are found, false otherwise.
     * @return hasErrors true if errors are found, false otherwise.
     ***************************************************************************/
    public boolean hasError()
    {
        return hasErrors;
    }

    /***************************************************************************
     * Returns the list of errors.
     * @return listOfErrors List of errors.
     ***************************************************************************/
    public ArrayList<String> getErrors()
    {
        return listOfErrors;
    }

    /***************************************************************************
     * Outputs the list of errors into error_output.txt
     ***************************************************************************/
    public void outputErrors()
    {
        if(!hasError()) return;
        try {
            PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(new File(System.getProperty("user.dir")+"\\error_output.txt"))));
            output.println("ERROR LIST:");
            for(int i = 0; i < listOfErrors.size(); i++){
                output.println(" - " + listOfErrors.get(i));
            }
            output.close();
        }catch (IOException ex){}
    }

    /***************************************************************************
     * Empties the list of errors.
     ***************************************************************************/
    public void clearErrors()
    {
        listOfErrors.clear();
        hasErrors = false;
    }
}
